DynamicQuant
将浮点输入张量按给定的 scale 和 zero point 量化为 INT8 输出张量。
\[q_i = \mathrm{clip}\left( \mathrm{round}\left( x_i \times \frac{1}{scale} + zp \right), -128, 127 \right)\]
其中:
\(x_i\) 为输入浮点值
\(scale\) 为量化比例因子
\(zp\) 为零点(zero point)
当输入为 \(+\infty\) 时,输出饱和到 127
当输入为 \(-\infty\) 时,输出饱和到 -128
- 输入:
real_values - 输入浮点数据地址。
quant_values - 输出 INT8 量化结果地址。
scale - 量化比例因子(标量)。
zp - 量化零点(标量,int32 类型)。
size - 输入元素总数。
min_value - 量化下界(通常为 -128)。
max_value - 量化上界(通常为 127)。
core_mask - 核掩码(仅适用于共享存储版本)。
- 输出:
quant_values - 量化后的 INT8 结果。
- 支持平台:
FT78NEMT7004
备注
FT78NE 支持 fp32 → int8
MT7004 支持 fp32, fp16 → int8
量化结果被限制在
[min_value, max_value]范围内(通常为[-128, 127])
共享存储版本:
-
void fp_dynamic_quant_s(float *real_values, int8_t *quant_values, float scale, int32_t zp, int size, int32_t min_value, int32_t max_value, int core_mask)
-
void hp_dynamic_quant_s(half *real_values, int8_t *quant_values, float scale, int32_t zp, int size, int32_t min_value, int32_t max_value, int core_mask)
C调用示例:
1#include <stdio.h>
2
3int main(int argc, char* argv[]) {
4 float *input = (float *)0x81000000;
5 int8_t *output = (int8_t *)0x82000000;
6 float scale = 0.1f;
7 int32_t zp = 0;
8 int size = 1024;
9 int32_t min_value = -128;
10 int32_t max_value = 127;
11 int core_mask = 0xff;
12
13 fp_dynamic_quant_s(input, output, scale, zp, size, min_value, max_value, core_mask);
14 return 0;
15}
私有存储版本:
-
void fp_dynamic_quant_p(float *real_values, int8_t *quant_values, float scale, int32_t zp, int size, int32_t min_value, int32_t max_value)
-
void hp_dynamic_quant_p(half *real_values, int8_t *quant_values, float scale, int32_t zp, int size, int32_t min_value, int32_t max_value)
C调用示例:
1#include <stdio.h>
2
3int main(int argc, char* argv[]) {
4 float *input = (float *)0x10010000;
5 int8_t *output = (int8_t *)0x10020000;
6 float scale = 0.1f;
7 int32_t zp = 0;
8 int size = 1024;
9 int32_t min_value = -128;
10 int32_t max_value = 127;
11
12 fp_dynamic_quant_p(input, output, scale, zp, size, min_value, max_value);
13 return 0;
14}